from PyQt4.QtCore import *
from PyQt4.QtGui import *

from qgis.core import *

class layerItem(QListWidgetItem):
    def __init__(self, layer):
        QListWidgetItem.__init__(self,layer.name(),None,1001)
        self.layer = layer
        return None
    
    
class fieldItem(QListWidgetItem):
    def __init__(self, field, layer):
        QListWidgetItem.__init__(self,field.name(),None,1002)
        self.layer = layer
        self.field = field
        return None

    def layerName(self):
        return self.layer.name()

    def fieldName(self):
        return self.field.name()
    
    def getData(self):
        data = []
        provider = self.layer.dataProvider()
        index = provider.fieldNameIndex(self.field.name())
        if self.layer.selectedFeatureCount() > 0:
            features = self.layer.selectedFeatures()
            [data.append(x.attributeMap()[index].toDouble()[0]) for x in features]
        else:
            for featureId in range(provider.featureCount()):
                feat = QgsFeature()
                provider.featureAtId(featureId,feat,True,[index])
                data.append(feat.attributeMap()[index].toDouble()[0])
        return data
        
            
